<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js正则表达式之原子组贪婪匹配</title>
  <style>
    input[name=password] {
      width:200px;
    }
    span.ok {
      color: green;
    }
    span.error {
      color: red;
    }
  </style>
</head>
<body>
  <ul>
    <li>贪婪匹配包含?,*,+,{a,b},?匹配0或1个,*匹配0到多个,+匹配1到多个,{a,b}代表匹配至少a个最多b个</li>
    <li>可批量应用正则对DOM元素内容进行验证(如用户名，密码验证)</li>
    <li>可使用贪婪匹配组合?(如:??,*?,+?,{a,}?,{a,b}?)禁止贪婪,尽量向较少数量的一侧进行匹配</li>
  </ul>
  <span class="ok">密码必须以字母开头，长度8到10位，至少包含一个大写字母，数字</span><br>
  <span>你当前输入的密码是：<span name="displayPassword" class="ok"></span></span><br>
  <input type="password" name="password" placeholder="请输入密码">
  <span name="message"></span>
  <div name="content">
    <span>https://github.com/miracle-git</span>
    <span>http://www.baidu.com</span>
    <span>https://www.google.com</span>
    <br>
    <button name="button">替换所有的span标签为h2标签</button>
  </div>
  <script>
    console.log('Miracle H'.match(/he?/i))        // ["H"]
    console.log('Miracle He'.match(/he?/i))       // ["He"]
    console.log('Miracle H'.match(/he*/i))        // ["H"]
    console.log('Miracle Hee'.match(/he*/i))      // ["Hee"]
    console.log('Miracle He'.match(/he+/i))       // ["He"]
    console.log('Miracle Hee'.match(/he+/i))      // ["Hee"]
    console.log('Miracle Hee'.match(/he{1,}/i))   // ["Hee"]
    console.log('Miracle Heee'.match(/he{2,4}/i)) // ["Heee"]
    // 利用贪婪匹配电话号码
    const reg1 = /^0\d{2,3}-\d{7,8}$/
    console.log('010-66666666'.match(reg1))       // ["010-66666666"]
    console.log('028-88888888'.match(reg1))       // ["028-88888888"]
    console.log('0755-99999999'.match(reg1))      // ["0755-99999999"]
    // 批量应用正则进行密码验证
    const password = document.querySelector('[name="password"]')
    const displayPassword = document.querySelector('[name="displayPassword"]')
    const message = document.querySelector('[name="message"]')
    const regs = [
      { pattern: /^[a-zA-Z]\w{7,9}$/, message: '密码不以字母开头或长度不对'},
      { pattern: /[A-Z]/, message: '密码至少包含一个大写字母'},
      { pattern: /[0-9]/, message: '密码至少包含一个数字'},
    ]
    password.addEventListener('keyup', function () {
      let err = ''
      const res = regs.every((item, index) => {
        const res = item.pattern.test(this.value)
        if (!res && !err) {
          err = item.message
        }
        return res
      })
      displayPassword.innerHTML = this.value
      message.innerHTML = res ? '正确' : err
      message.classList.remove('ok', 'error')
      message.classList.add(res ? 'ok' : 'error')
    })
    // 禁止贪婪
    console.log('Miracle Hee'.match(/he??/i))       // ["H"]
    console.log('Miracle Hee'.match(/he*?/i))       // ["H"]
    console.log('Miracle Hee'.match(/he+?/i))       // ["He"]
    console.log('Miracle Heee'.match(/he{2,4}?/i))  // ["Hee"]
    // 替换所有的span标签
    const content = document.querySelector('[name="content"]')
    const button = document.querySelector('[name="button"]')
    const reg2 = /<span>([\s\S]+?)<\/span>/gi
    button.addEventListener('click', function () {
      content.innerHTML = content.innerHTML.replace(reg2, `<h2>$1</h2>`)
    })
  </script>
</body>
</html>